/**
 * 
 */
package org.compiere.process;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.model.Mhrmtimesheet;
import org.compiere.util.Env;
import org.compiere.util.Msg;

/**
 * @author A.Yerzhanov
 * 25.10.2012
 */
public class GenerateTimesheet extends SvrProcess {
	
	/** Current context					*/
	private Properties m_ctx;
	/**	 */
	private ProcessInfo pi;	
	/** */
	private int C_BPartner_ID;
	private Timestamp dt = null;
	/** */
	private int hrm_workday1;
	private int hrm_workday2;
	private int hrm_workday3;
	private int hrm_workday4;
	private int hrm_workday5;
	private int hrm_workday6;
	private int hrm_workday7;
	private int hrm_workday8;
	private int hrm_workday9;
	private int hrm_workday10;
	private int hrm_workday11;
	private int hrm_workday12;	
	/** */
	private Timestamp dateFrom1;
	private Timestamp dateFrom2;
	private Timestamp dateFrom3;
	private Timestamp dateFrom4;
	private Timestamp dateFrom5;
	private Timestamp dateFrom6;
	private Timestamp dateFrom7;
	private Timestamp dateFrom8;
	private Timestamp dateFrom9;
	private Timestamp dateFrom10;
	private Timestamp dateFrom11;
	private Timestamp dateFrom12;
	
	/** Get month and year */
	Calendar calendar = Calendar.getInstance();
	
	/** Time sheet table */
	private Mhrmtimesheet time = null;
	/** */

	/* (non-Javadoc)
	 * @see org.compiere.process.SvrProcess#prepare()
	 */
	@Override
	protected void prepare() {
		
		log.info("");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
				String name = para[i].getParameterName();
				if (para[i].getParameter() == null); 
				else if (name.compareTo("C_BPartner_ID") == 0)
					C_BPartner_ID = Integer.parseInt(para[i].getParameter().toString());
				// 1
				else if (name.toLowerCase().equals("datefrom1"))
					dateFrom1 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday1") == 0)
					hrm_workday1 = Integer.parseInt(para[i].getParameter().toString());
				// 2
				else if (name.toLowerCase().equals("datefrom2"))
					dateFrom2 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday2") == 0)
					hrm_workday2 = Integer.parseInt(para[i].getParameter().toString());
				// 3
				else if (name.toLowerCase().equals("datefrom3"))
					dateFrom3 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday3") == 0)
					hrm_workday3 = Integer.parseInt(para[i].getParameter().toString());
				// 4
				else if (name.toLowerCase().equals("datefrom4"))
					dateFrom4 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday4") == 0)
					hrm_workday4 = Integer.parseInt(para[i].getParameter().toString());
				// 5
				else if (name.toLowerCase().equals("datefrom5"))
					dateFrom5 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday5") == 0)
					hrm_workday5 = Integer.parseInt(para[i].getParameter().toString());
				// 6
				else if (name.toLowerCase().equals("datefrom6"))
					dateFrom6 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday6") == 0)
					hrm_workday6 = Integer.parseInt(para[i].getParameter().toString());
				// 7
				else if (name.toLowerCase().equals("datefrom7"))
					dateFrom7 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday7") == 0)
					hrm_workday7 = Integer.parseInt(para[i].getParameter().toString());
				// 8
				else if (name.toLowerCase().equals("datefrom8"))
					dateFrom8 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday8") == 0)
					hrm_workday8 = Integer.parseInt(para[i].getParameter().toString());
				// 9
				else if (name.toLowerCase().equals("datefrom9"))
					dateFrom9 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday9") == 0)
					hrm_workday9 = Integer.parseInt(para[i].getParameter().toString());
				// 10
				else if (name.toLowerCase().equals("datefrom10"))
					dateFrom10 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday10") == 0)
					hrm_workday10 = Integer.parseInt(para[i].getParameter().toString());
				// 11
				else if (name.toLowerCase().equals("datefrom11"))
					dateFrom11 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday11") == 0)
					hrm_workday11 = Integer.parseInt(para[i].getParameter().toString());
				// 12
				else if (name.toLowerCase().equals("datefrom12"))
					dateFrom12 = (Timestamp) para[i].getParameter();
				else if (name.compareTo("hrm_workday12") == 0)
					hrm_workday12 = Integer.parseInt(para[i].getParameter().toString());		
				else
				{
					log.log(Level.SEVERE, "Unknown Parameter: " + name);
				}
			}	
	}

	/* (non-Javadoc)
	 * @see org.compiere.process.SvrProcess#doIt()
	 */
	@Override
	protected String doIt() throws Exception {
		/** set constant */
		int QUOTA = 8;
		int SHEDULE = 1000008;
		int OVER = 0;
		/** set array list */
		Timestamp[] dateArray = {dateFrom1, dateFrom2, dateFrom3, dateFrom4, dateFrom5, dateFrom6, dateFrom7, dateFrom8, dateFrom9, dateFrom10, dateFrom11, dateFrom12};
		int[] workdayArray = {hrm_workday1, hrm_workday2, hrm_workday3, hrm_workday4, hrm_workday5, hrm_workday6, hrm_workday7, hrm_workday8, hrm_workday9, hrm_workday10, hrm_workday11, hrm_workday12};
		
		for (int i=0; i < dateArray.length; i ++){
			if (workdayArray[i] != 0){
				for (int j=0; j < workdayArray[i]; j ++){ 
					calendar.setTime(dateArray[i]);
					calendar.set(calendar.DAY_OF_MONTH, j+1);
					dt = new Timestamp(calendar.getTimeInMillis());
					time = Mhrmtimesheet.getTimeSheet(C_BPartner_ID, dt);
					if (time == null) {
					  time = new Mhrmtimesheet(getCtx(), getRecord_ID(), null);
						// set rows
						time.setAD_Org_ID(Env.getAD_Org_ID(getCtx()));
						time.setC_BPartner_ID(C_BPartner_ID);
						time.sethrm_hourquota(QUOTA);
						time.sethrm_shedulerule_ID(SHEDULE);
						time.setDateFrom(dt);
						time.sethrm_OvertimeHour(OVER);
						time.saveEx();
					}
					calendar.clear();
				}
			}
		}
		
		return Msg.translate(m_ctx, "Success");
	}

}
